using System;
using System.Data;
using DayPilot.Utils;
using DayPilot.Web.Ui.Enums;

public partial class Scheduler_Week : System.Web.UI.Page
{
    private DataTable table;
    protected void Page_Load(object sender, EventArgs e)
    {

        #region Data loading initialization
        if (Session["ResourcesView"] == null)
        {
            Session["ResourcesView"] = DataGenerator.GetData();
        }
        table = (DataTable)Session["ResourcesView"];
        DayPilotScheduler1.DataSource = Session["ResourcesView"];
        DayPilotNavigator1.DataSource = Session["ResourcesView"];
        #endregion

        if (!IsPostBack)
        {
            DayPilotScheduler1.StartDate = Week.FirstDayOfWeek();
            DataBind();
        }

    }

    protected void DayPilotScheduler1_EventMove(object sender, DayPilot.Web.Ui.Events.EventMoveEventArgs e)
    {
        #region Simulation of database update

        DataRow dr = table.Rows.Find(e.Value);
        if (dr != null)
        {
            dr["start"] = e.NewStart;
            dr["end"] = e.NewEnd;
            dr["column"] = e.NewResource;
            table.AcceptChanges();
        }

        #endregion

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();
    }
    protected void DayPilotScheduler1_EventResize(object sender, DayPilot.Web.Ui.Events.EventResizeEventArgs e)
    {
        #region Simulation of database update
        DataRow dr = table.Rows.Find(e.Value);
        if (dr != null)
        {
            dr["start"] = e.NewStart;
            dr["end"] = e.NewEnd;
            table.AcceptChanges();
        }
        #endregion

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();
    }

    protected void DayPilotScheduler1_TimeRangeSelected(object sender, DayPilot.Web.Ui.Events.TimeRangeSelectedEventArgs e)
    {
        #region Simulation of database update
        DataRow dr = table.NewRow();
        dr["start"] = e.Start;
        dr["end"] = e.End;
        dr["id"] = Guid.NewGuid().ToString();
        dr["name"] = "New event";
        dr["column"] = e.ColumnId;

        table.Rows.Add(dr);
        table.AcceptChanges();
        #endregion

        DayPilotScheduler1.DataBind();
        DayPilotScheduler1.Update();
    }

    protected void DayPilotScheduler1_Command(object sender, DayPilot.Web.Ui.Events.CommandEventArgs e)
    {
        switch (e.Command)
        {
            case "navigate":
                DateTime start = (DateTime)e.Data["start"];
                DateTime end = (DateTime)e.Data["end"];

                DayPilotScheduler1.StartDate = start;
                DayPilotScheduler1.DataBind();
                DayPilotScheduler1.Update(CallBackUpdateType.Full);
                break;
        }
    }

    protected void DayPilotNavigator1_VisibleRangeChanged(object sender, DayPilot.Web.Ui.Events.Navigator.VisibleRangeChangedEventArgs e)
    {
        DayPilotNavigator1.DataBind();
    }
}
